import { Message } from 'element-ui'
import { bindUkey } from '@/api/system/adminConfig/administrators'
import vm from '../main.js'

var ctrl = null
var websock = true
var b = new Base64()
var ArmHandle = null // 加密锁句柄
var Index = 0
var pub_write = null
var name_write = null
export function OnLoad () {
  return new Promise((resolve, reject) => {
    try {
      ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
      resolve(ctrl)
    } catch (e) {
      ctrl = null
      websock = false
      Message.error(vm.$t('绑定ukey失败'))
      resolve()
    }
  })
}

// 查找RockeyArm
export function Arm_Enum (data) {
  pub_write = data[1]
  name_write = data[2]
  if (!websock) {
    try {
      ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
      ctrl.Arm_Enum()
    } catch (e) {
      Message.error('绑定ukey失败')
    }
    // var rtn = ctrl.Arm_Enum()
  } else {
    try {
      ctrl.Arm_Enum(function (result, response) {
        if (!result) {
          Message.error('Ukey验证失败, 请插入正确Ukey, 再次验证')
          // Message.error('Arm_Enum error. ' + response)
          // alert('Arm_Enum error. ' + response);
        } else {
          // var rtn = response
          Arm_Open(data[0])
        }
      })
    } catch (e) {
      ctrl = null
      websock = false
      Message.error(vm.$t('绑定ukey失败'))
      // Message.error(e)
    }
  }
}

// 打开RockeyArm
export function Arm_Open (key) {
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    // ctrl = document.getElementById('ctrl');
    ArmHandle = ctrl.Arm_Open(parseInt(Index))
  } else {
    ctrl.Arm_Open(function (result, response) {
      if (!result) {
        Message.error('Arm_Enum error. ' + response)
        // alert('Arm_Enum error. ' + response);
      } else {
        ArmHandle = response
        Arm_VerifyPIN(key)
      }
    })
  }
}

// 验证PIN码
export function Arm_VerifyPIN (key) {
  var Handle = ArmHandle
  var UserPin = 'likonghuakang123'
  var UserType = 1
  // 获取要验证的用户类型
  if (!websock) {
    // ctrl = document.getElementById('ctrl');
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_VerifyPIN(parseInt(Handle), UserType, UserPin)
    if (rtn === '0') {
      Message.success(vm.$t('验证PIN码成功'))
      // alert('<spring:message code='验证PIN码成功'></spring:message>')
    } else {
      Message.error(vm.$t('uKey验证未通过'))
      // alert('<spring:message code='uKey验证未通过'></spring:message>')
      // alert(rtn);
      // alert('uKey验证!');
    }
  } else {
    ctrl.Arm_VerifyPIN(function (result, response) {
      if (!result) {
        Message.error('Arm_VerifyPIN error. ' + response)
        // alert('Arm_VerifyPIN error. ' + response);
      } else {
        var rtn = response
        if (rtn === '0') {
          Arm_WriteFile(key)
        } else {
          // alert('uKey验证!');
          // alert(rtn);
          Message.error(vm.$t('请插入ukey'))
          // alert('请插入ukey');
        }
      }
    }, parseInt(Handle), UserPin, UserType)
  }
}

// 写文件
export function Arm_WriteFile (key) {
  var Handle = ArmHandle
  var FileOffset = 0
  var DataInput = b.encode(key)
  // var DataInput = key
  var FileType = 1
  var FileID = 1

  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_WriteFile(parseInt(Handle), FileType, parseInt(FileID), parseInt(FileOffset), DataInput)
    if (rtn === '0') {
      Message.success(vm.$t('写文件件成功'))
      // alert('<spring:message code='写文件件成功'></spring:message>');
    } else {
      // alert(rtn);
      Message.error(vm.$t('uKey验证未通过！'))
      // alert('<spring:message code='uKey验证未通过!'></spring:message>');
    }
  } else {
    ctrl.Arm_WriteFile(function (result, response) {
      if (!result) {
        Message.error('Arm_WriteFile error. ' + response)
        // alert('Arm_WriteFile error. ' + response);
      } else {
        var rtn = response
        if (rtn === '0') {
          Message.success(vm.$t('绑定成功'))
          Arm_Close()
        } else {
          // alert(rtn);
          Message.error(vm.$t('uKey验证未通过！'))
          // alert('<spring:message code='uKey验证未通过!'></spring:message>');
        }
      }
    }, parseInt(Handle), FileType, FileID, FileOffset, DataInput)
  }
}

// 关闭RockeyArm
export function Arm_Close () {
  var Handle = ArmHandle
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    // ctrl = document.getElementById('ctrl');
    var rtn = ctrl.Arm_Close(parseInt(Handle))
    if (rtn === '0') {
      Message.success(vm.$t('关闭锁成功'))
      // alert('<spring:message code='关闭锁成功'></spring:message>');
    } else {
      // alert(rtn);
      Message.error(vm.$t('uKey验证未通过！'))
      // alert('<spring:message code='uKey验证未通过!'></spring:message>');
    }
  } else {
    ctrl.Arm_Close(function (result, response) {
      if (!result) {
        Message.error('Arm_Close error. ' + response)
        // alert('Arm_Close error. ' + response);
      } else {
        var rtn = response
        if (rtn === '0') {
          var form = {
            name: name_write,
            public_key: pub_write
          }
          bindUkey(form).then(res => {
            if (res.status !== 0) {
              Message.error(vm.$t('后台绑定失败'))
            }
          })
          // $.ajax({
          //   type: 'POST',
          //   url: '${ctx}/system/adminConfig/bindUkey',
          //   dataType: 'json',
          //   data: { ukey: pub, name: name },
          //   async: false,
          //   success: function (result) {
          //     if (result.success == true) {
          //       layer.msg(result.msg);
          //     } else {
          //       layer.alert(result.msg);
          //     }

          //   }
          // });
        } else {
          // alert(rtn);
          Message.error(vm.$t('uKey验证未通过！'))
          // alert('<spring:message code='uKey验证未通过!'></spring:message>');
        }
      }
    }, parseInt(Handle))
  }
}

export default {
  // Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器，第二个参数是一个可选的选项对象。
  install: function (Vue) {
    Vue.prototype.OnLoadWrite = (param) => OnLoad(param)
    Vue.prototype.Arm_Enum_Write = (param) => Arm_Enum(param)
  }
}
